package com.fw.service.merchantinterface.impl;

import java.math.BigDecimal;
import java.util.List;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.fw.service.inboundmessagelog.InboundMessageLogService;
import com.fw.service.inboundmessagelog.bean.InboundMessageLogBean;
import com.fw.service.interfacevalidator.InterfaceValidatorService;
import com.fw.service.merchantinterface.MerchantInterfaceService;
import com.fw.service.merchanttransactiondata.MerchantTransactionDataService;
import com.fw.service.merchanttransactiondata.bean.MerchantTransactionDataBean;
import com.fw.service.persistence.dto.Error;
import com.fw.util.StringUtils;

public class MerchantInterfaceServiceImpl implements MerchantInterfaceService {

    private static final Logger log = Logger.getLogger(MerchantInterfaceServiceImpl.class);

    @Autowired
    private InboundMessageLogService inboundMessageLogService;

    @Autowired
    private InterfaceValidatorService interfaceValidatorService;

    @Autowired
    private MerchantTransactionDataService merchantTransactionDataService;

    @Override
    public Boolean receiveInboundMessageLog(InboundMessageLogBean inboundMessageLogBean) {
	Boolean success = Boolean.FALSE;
	try {
	    inboundMessageLogBean = inboundMessageLogService.createInboundMessageLog(inboundMessageLogBean);
	    if (inboundMessageLogBean == null || inboundMessageLogBean.getId() == null) {
		success = Boolean.FALSE;
	    } else {
		List<Error> errors = interfaceValidatorService.validate(inboundMessageLogBean);
		if (errors != null && !errors.isEmpty()) {
		    for (Error error : errors) {
			log.warn(error.getErrorCode());
			System.err.println("validate error => " + error.getErrorCode());
		    }
		    success = Boolean.FALSE;
		} else {
		    MerchantTransactionDataBean merchantTransactionDataBean = new MerchantTransactionDataBean();
		    // TODO: must init value
		    merchantTransactionDataBean.setMerchantId(inboundMessageLogBean.getMerchantID());
		    String invoiceNo = StringUtils
			    .getCashFormat(inboundMessageLogBean.getServiceID(), inboundMessageLogBean.getInvoiceNo());
		    merchantTransactionDataBean.setInvoiceNo(invoiceNo);
		    merchantTransactionDataBean.setCurrencyCode(inboundMessageLogBean.getCurrencyCode());
		    merchantTransactionDataBean.setLang(inboundMessageLogBean.getLanguage());
		    merchantTransactionDataBean.setAmount(new BigDecimal(inboundMessageLogBean.getAmount()));
		    merchantTransactionDataBean.setCustomerName(inboundMessageLogBean.getCustomerName());
		    merchantTransactionDataBean.setCustomerEmail(inboundMessageLogBean.getCustomerEmail());
		    merchantTransactionDataBean.setCustomerPhone(inboundMessageLogBean.getCustomerPhone());
		    merchantTransactionDataBean.setInboundMessageLogId(inboundMessageLogBean.getId());
		    merchantTransactionDataBean = merchantTransactionDataService.createMerchantTransactionData(merchantTransactionDataBean);
		    if (merchantTransactionDataBean == null || merchantTransactionDataBean.getId() == null) {
			success = Boolean.FALSE;
		    } else {
			success = Boolean.TRUE;
		    }
		}
	    }
	} catch (Exception e) {
	    success = Boolean.FALSE;
	    e.printStackTrace();
	}
	return success;
    }
}
